﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SomethingSpacial.Presenters;

namespace SomethingSpacial.Views.Default
{
    public partial class RSS : UserControl
    {
        public RSS()
        {
            InitializeComponent();
            MaxCount = 5;
        }

        #region [ dependency properties ]

        public static readonly DependencyProperty MaxCountProperty = DependencyProperty.Register("MaxCount", typeof(int), typeof(RSS), new PropertyMetadata(new PropertyChangedCallback(OnMaxCountChanged)));

        public int MaxCount
        {
            get { return (int)GetValue(MaxCountProperty); }
            set { SetValue(MaxCountProperty, value); }
        }

        private static void OnMaxCountChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            RSS ThisElement = DpObj as RSS;

            if (ThisElement != null)
            {

            }
        }

        public static readonly DependencyProperty FeedAddressProperty = DependencyProperty.Register("FeedAddress", typeof(string), typeof(RSS), new PropertyMetadata(new PropertyChangedCallback(OnFeedAddressChanged)));

        public string FeedAddress
        {
            get { return (string)GetValue(FeedAddressProperty); }
            set { SetValue(FeedAddressProperty, value); }
        }

        private static void OnFeedAddressChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            RSS ThisElement = DpObj as RSS;

            if (ThisElement != null)
            {
                FeedPresenter NewFeedPresenter = new FeedPresenter();
                NewFeedPresenter.PresentA2Feed((ItemsControl)(ThisElement.FindName("FeedList")), (ItemsControl)(ThisElement.FindName("FeedList2")), ThisElement.FeedAddress, 1, ThisElement.MaxCount);
            }
        }

        public static readonly DependencyProperty DisplayTitleProperty = DependencyProperty.Register("DisplayTitle", typeof(string), typeof(RSS), new PropertyMetadata(new PropertyChangedCallback(OnDisplayTitleChanged)));

        public string DisplayTitle
        {
            get { return (string)GetValue(DisplayTitleProperty); }
            set { SetValue(DisplayTitleProperty, value); }
        }

        private static void OnDisplayTitleChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            RSS ThisElement = DpObj as RSS;

            if (ThisElement != null)
            {
                TextBlock DisplayTitleTarget = (TextBlock)(ThisElement.FindName("DisplayTitleTarget"));
                DisplayTitleTarget.Text = ThisElement.DisplayTitle;
            }
        }

        public static readonly DependencyProperty BackSideProperty = DependencyProperty.Register("BackSide", typeof(bool), typeof(RSS), new PropertyMetadata(new PropertyChangedCallback(OnBackSideChanged)));

        public bool BackSide
        {
            get { return (bool)GetValue(BackSideProperty); }
            set { SetValue(BackSideProperty, value); }
        }

        private static void OnBackSideChanged(DependencyObject DpObj, DependencyPropertyChangedEventArgs e)
        {
            RSS ThisElement = DpObj as RSS;

            if (ThisElement != null)
            {
                TextBlock MoreTxtBlck = (TextBlock)(ThisElement.FindName("MoreTxtBlck"));
                TextBlock FrontTxtBlck = (TextBlock)(ThisElement.FindName("FrontTxtBlck"));
                if (ThisElement.BackSide)
                {
                    MoreTxtBlck.Visibility = Visibility.Visible;
                    FrontTxtBlck.Visibility = Visibility.Visible;
                }
                else
                {
                    MoreTxtBlck.Visibility = Visibility.Collapsed;
                    FrontTxtBlck.Visibility = Visibility.Collapsed;
                }
            }
        }

        #endregion

        private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void MoreTxtBlck_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Flipper.ExecuteFlip();
        }
    }
}
